{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#对于标签，将特征处理成历史特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "import xgboost as xgb\n",
    "\n",
    "window_size=3\n",
    "\n",
    "PATH_PROCESSED_DATA = 'processed/'\n",
    "FILENAME_TESTSET = PATH_PROCESSED_DATA+f'data_past{window_size}_test.csv'\n",
    "FILENAME_TRAIN_AND_VALID = PATH_PROCESSED_DATA+f'train_valid_set_combined_139.csv'\n",
    "PATH_TESTSET_PREDICTIONS = 'predict_tables/'\n",
    "\n",
    "data_test = pd.read_csv(FILENAME_TESTSET)\n",
    "data_train_and_valid = pd.read_csv(FILENAME_TRAIN_AND_VALID)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#评价指标WMAPE\n",
    "def compute_sum_diff_and_sum_real_and_wmape(actuals,predictions):\n",
    "    actuals=list(actuals)\n",
    "    predictions=list(predictions)\n",
    "    len_predicitons = len(predictions)\n",
    "    sum_diff = 0.0\n",
    "    sum_real = sum([abs(i) for i in actuals])\n",
    "    \n",
    "    for i in range(len_predicitons):\n",
    "        pred = predictions[i]\n",
    "        real = actuals[i]\n",
    "        sum_diff += abs(pred - real)\n",
    "    #若分母为0，判断分子。若分子为0，输出0,；若分子不为0，输出100.\n",
    "    if sum_real==0:\n",
    "        if sum_diff==sum_real:\n",
    "            return sum_diff,sum_real,0\n",
    "        else:\n",
    "            return sum_diff,sum_real,100\n",
    "    else:\n",
    "        wmape =sum_diff/ sum_real\n",
    "        return sum_diff,sum_real,wmape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cols_past= ['apply_amt', 'redeem_amt', 'net_in_amt', 'uv_fundown','uv_stableown','uv_fundopt','uv_fundmarket','uv_termmarket', 'total_net_value', 'yield']\n",
    "cols_past_combined=[f'{col}_{i}' for i in range(1,window_size+1) for col in cols_past]\n",
    "cols_now=['forecast_step','is_week_end','is_month_end','num_days_from_base']\n",
    "cols_x=cols_past_combined+cols_now\n",
    "\n",
    "cols_y=['apply_amt_pred', 'redeem_amt_pred', 'net_in_amt_pred']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#repeat\n",
    "list_product_pid=data_test['product_pid'].unique()\n",
    "for product_pid in list_product_pid:\n",
    "    data_this_product_test=data_test[data_test['product_pid']==product_pid]\n",
    "    data_this_product_train_and_valid=data_train_and_valid[data_train_and_valid['product_pid']==product_pid]\n",
    "    pred_apply = data_this_product_train_and_valid['apply_amt'].iloc[-1]\n",
    "    pred_redeem = data_this_product_train_and_valid['redeem_amt'].iloc[-1]\n",
    "    pred_net_in = data_this_product_train_and_valid['net_in_amt'].iloc[-1]\n",
    "\n",
    "    # 将预测结果保存到data中对应的列中\n",
    "    data_test.loc[data_test['product_pid']==product_pid,\"apply_amt_pred\"] = pred_apply\n",
    "    data_test.loc[data_test['product_pid']==product_pid,\"redeem_amt_pred\"] = pred_redeem\n",
    "    data_test.loc[data_test['product_pid']==product_pid,\"net_in_amt_pred\"] = pred_net_in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#保存测试集的预测结果到本地\n",
    "way_steps=None\n",
    "way_past=1\n",
    "way_feature=None\n",
    "way_model='repeat'\n",
    "way_split=None\n",
    "way_test='applyDirectRedeemDirectNetDirect'\n",
    "data_test.to_csv(PATH_TESTSET_PREDICTIONS+f\"predict_table_feature-{way_feature}_model-{way_model}_split-{way_split}_test-{way_test}_steps-{way_steps}_past-{way_past}.csv\",index=False,columns=['product_pid','transaction_date','apply_amt_pred','redeem_amt_pred','net_in_amt_pred'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "AFAC2023ApplyAndRedeem_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
